package com.mall.common.validation;

import java.math.BigDecimal;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import com.mall.common.bean.WebErrors;
import com.mall.common.plugin.AutoRegisterPlugin;
import com.mall.common.validation.annotation.IntRangeFieldValidator;
import com.mall.common.validation.annotation.Validations;

public class IntRangeValidate extends AutoRegisterPlugin implements IValidate {

	@Override
	public WebErrors validate(HttpServletRequest request, WebErrors errors,
			Validations validations, Map<String, String> decodedUriVariables) {
		String regex = "(\\d(.\\d)*)*";
		IntRangeFieldValidator[] irfvs = validations.intRangeFields();
		for(IntRangeFieldValidator irfv : irfvs){
			String fieldName = irfv.fieldName();
			String value = request.getParameter(fieldName);
			if(StringUtils.isEmpty(value) && decodedUriVariables != null){
				value = decodedUriVariables.get(fieldName);
			}
			if(StringUtils.isEmpty(value) 
					|| !Pattern.matches(regex, value)){
				errors.addErrorString(irfv.message());
				continue;
			}
			BigDecimal number = new BigDecimal(value);
			String minStr = irfv.min();
			if(!StringUtils.isBlank(minStr)){
				if(Pattern.matches(regex, minStr)){
					BigDecimal min = new BigDecimal(minStr);
					if(min.compareTo(number) > 0){
						errors.addErrorString(irfv.message());
						continue;
					}
				}else{
					errors.addErrorString(irfv.message());
					continue;
				}
			}
			String maxStr = irfv.max();
			if(!StringUtils.isBlank(maxStr)){
				if(Pattern.matches(regex, maxStr)){
					BigDecimal max = new BigDecimal(maxStr);
					if(max.compareTo(number) < 0){
						errors.addErrorString(irfv.message());
						continue;
					}
				}else{
					errors.addErrorString(irfv.message());
					continue;
				}
			}
		}
		return errors;
	}

	@Override
	public String getType() {
		return "vallidate";
	}

	@Override
	public String getId() {
		return "intRangeValidate";
	}

	@Override
	public String getName() {
		return "数值区间校验";
	}

	@Override
	public String getVersion() {
		return "1.0.0";
	}

	@Override
	public String getAuthor() {
		return "fangchen";
	}

	@Override
	public void perform(Object... params) {
		
	}

	@Override
	public void register() {
		
	}
}
